import { _decorator, assetManager, Component, ImageAsset, Node, SpriteFrame } from 'cc';
import { Globle } from './Globle';
const { ccclass, property } = _decorator;

@ccclass('Utils')
export class Utils{
    /**获取URL上的所有数据 */
    public static getUrlAllData(){
        let parameterPos = window.location.href.indexOf("?");
        if(parameterPos>0){
            let parameter = window.location.href.substr(parameterPos+1);
            let dataArr = this.stringToParameter(parameter);
            Globle.urlDataArr = dataArr;
        }
        if (this.getUrlDataByName("id")) Globle.userData.id = this.getUrlDataByName("id");
        if (this.getUrlDataByName("nickName")) Globle.userData.nickName = this.getUrlDataByName("nickName");
        if (this.getUrlDataByName("avatar")) Globle.userData.avatar = this.getUrlDataByName("avatar");
    }

    /**
     * 解析参数字符串
     * @param parameter 字符串
     * @returns 
     */
    public static stringToParameter(parameter:string)
    {
        let parameters = parameter.split("&");
        let dataArr = [];
        for (let i = 0; i < parameters.length; i++) {
            let item = parameters[i].split("=");
            let data = {
                key:item[0],
                value:item[1]
            }
            dataArr.push(data);
        }
        return dataArr;
    }

    /**根据名字获取url里的属性 */
   static getUrlDataByName(name:string){
       if(Globle.urlDataArr == undefined) return null;
       for (let i = 0; i < Globle.urlDataArr.length; i++) {
           let data = Globle.urlDataArr[i];
           if(data.key == name){
               return data.value;
           }
       }
       return null;
   }
}
